﻿///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2009 David Hill. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows;
using System.Linq;
using Microsoft.Practices.Composite;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.UnityExtensions;

using Dashboard.Shell.Views;
using AuthenticationService.Client;
using AuthenticationService.Web;
using System.Windows.Ria.ApplicationServices;
using System.Collections.Generic;
using ModuleManagerService.Web;

namespace Dashboard.Shell
{
    public partial class Bootstrapper : UnityBootstrapper
    {
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            Container.RegisterInstance(this);
        }

        public IEnumerable<Module> ModuleList { get; set; }

        protected override IModuleCatalog GetModuleCatalog()
        {
            RegisterViewsAndServices();

            //return ModuleCatalog.CreateFromXaml(new Uri("/Dashboard.Shell;component/ModuleCatalog.xaml", UriKind.Relative));

            //Only get the Modules from the Core Category.  
            IEnumerable<ModuleInfo> miList = (from mi in ModuleList
                                              where mi.CategoryName == "Core"
                                              select new ModuleInfo(mi.Name, mi.Type)
                                              {
                                                  InitializationMode = (Microsoft.Practices.Composite.Modularity.InitializationMode)Enum.Parse(typeof(Microsoft.Practices.Composite.Modularity.InitializationMode), mi.InitializationMode.Name, false),
                                                  Ref = mi.Path
                                              });
            Container.RegisterInstance(miList);
            return new ModuleCatalog(miList);
        }

        public void PostCatalogProcessing()
        {
             ModuleCatalog catalog = (ModuleCatalog)Container.Resolve<IModuleCatalog>();

            //Only get the Modules that are not Core and are not already in the Catalog 
            IEnumerable<ModuleInfo> miList = (from mi in ModuleList
                                              where mi.CategoryName != "Core"
                                              && catalog.Modules.Any(m => m.ModuleName == mi.Name) == false
                                              select new ModuleInfo(mi.Name, mi.Type)
                                              {
                                                  InitializationMode = (Microsoft.Practices.Composite.Modularity.InitializationMode)Enum.Parse(typeof(Microsoft.Practices.Composite.Modularity.InitializationMode), mi.InitializationMode.Name, false),
                                                  Ref = mi.Path
                                              });
            foreach (ModuleInfo m in miList)
            {
                catalog.AddModule(m);
            }
            InitializeModules();
        }
        
        protected override DependencyObject CreateShell()
        {
            // Use the container to create an instance of the shell.
            ShellView view = Container.Resolve<ShellView>();

            // Display the Shell as the root visual for the application.
            Application.Current.RootVisual = view;

            return view;
        }

        protected void RegisterViewsAndServices()
        {
            ((WebAuthenticationService)WebContext.Current.Authentication).DomainContext =
                    new AuthenticationContext();
            Container.RegisterInstance<WebContext>(WebContext.Current);
            WebContext.Current.Authentication.LoggedIn += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedIn);
            WebContext.Current.Authentication.LoggedOut += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        void Authentication_LoggedOut(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
            PostCatalogProcessing();
        }

        void Authentication_LoggedIn(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
            PostCatalogProcessing();
        }

    }
}

